package admin

import (
	"common"
	"common/biz"
	"common/logs"
	"core/models/entity"
	"core/repo"
	"gate/types"
	"net/http"

	"github.com/gin-gonic/gin"
)

type GameApi struct {
	Manager *repo.Manager
}

func NewGameApi(repo *repo.Manager) *GameApi {
	return &GameApi{
		Manager: repo,
	}
}

func (g *GameApi) Add(ctx *gin.Context) {
	var game types.GameAddReq
	if err := ctx.ShouldBindJSON(&game); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"msg": "Invalid request params"})
		return
	}
	if game.Appid == "" || game.Secret == "" || game.Name == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "appid, secret, and name cannot be empty"})
		return
	}
	// 查询是否存在一样的了
	var gameM entity.GameM

	res := g.Manager.Mysql.GOrm.Where("appid=?", game.Appid).Find(&gameM)
	if res.Error != nil {
		logs.Error("Error occurred during query:", res.Error)
		return
	}
	if len(gameM.Appid) > 0 {
		common.Success(ctx, gin.H{
			"code": 1,
			"msg":  "数据已存在",
		})
		return
	}
	gameM.Appid = game.Appid
	gameM.Name = game.Name
	gameM.Secret = game.Secret
	err := g.Manager.Mysql.GOrm.Create(&gameM).Error
	if err != nil {
		logs.Error("game insert err", err)
		common.Fail(ctx, biz.SqlError)
		return
	}
	common.Success(ctx, map[string]any{
		"game": gameM,
	})
	return
	// one := &entity.Game{
	// 	Name:   game.Name,
	// 	Appid:  game.Appid,
	// 	Secret: game.Secret,
	// }
	// logs.Info("one:%v", one)
	// table := g.Manager.Mongo.Db.Collection("sy_games")
	// filters := []bson.M{}
	// filters = append(filters, bson.M{"appid": game.Appid})
	// var result entity.Game
	// err := table.FindOne(ctx, bson.M{"$and": filters}).Decode(&result)
	// if err != nil && err != mongo.ErrNoDocuments {
	// 	common.Fail(ctx, biz.SqlError)
	// 	return
	// }
	// if len(result.Appid) > 0 {
	// 	common.Success(ctx, gin.H{
	// 		"code": 1,
	// 		"msg":  "数据已存在",
	// 	})
	// 	return
	// }

	// one = &entity.Game{
	// 	Name:   game.Name,
	// 	Appid:  game.Appid,
	// 	Secret: game.Secret,
	// }
	// logs.Info("one:%v", one)

	// cursor, err1 := table.InsertOne(ctx, one)
	// if err1 != nil {
	// 	ctx.JSON(http.StatusBadRequest, gin.H{"error": "db err;" + err.Error()})
	// 	return
	// }
	// one.Id, _ = cursor.InsertedID.(primitive.ObjectID)
	// common.Success(ctx, map[string]any{
	// 	"game": one,
	// })

}

func (g *GameApi) List(c *gin.Context) {
	// 获取参数
	name := c.Query("name")
	appid := c.Query("appid")
	id := c.Query("id")

	var games []entity.GameM
	query := g.Manager.Mysql.GOrm
	if len(name) != 0 {
		query.Where("name like ?", "%"+name+"%")
	}
	if len(id) != 0 {
		query.Where("id = ?", id)
	}
	if len(appid) != 0 {
		query.Where("appid=?", appid)
	}
	err := query.Find(&games).Error

	if err != nil {
		common.Fail(c, biz.SqlError)
		return
	}
	common.Success(c, games)

	// var filters []interface{}
	// if name != "" {
	// 	filters = append(filters, bson.M{"name": name})
	// }
	// if appid != "" {
	// 	filters = append(filters, bson.M{"appid": appid})
	// }
	// // 如果没有查询参数，返回错误
	// if len(filters) == 0 {
	// 	c.JSON(http.StatusBadRequest, gin.H{"error": "No query parameters provided"})
	// 	return
	// }
	// var games []entity.Game
	// table := g.Manager.Mongo.Db.Collection("sy_games")
	// cursor, err := table.Find(c, bson.M{"$or": filters})
	// if err != nil {
	// 	c.JSON(http.StatusOK, gin.H{"msg": "success", "data": []int{}})
	// 	return
	// }
	// defer cursor.Close(c)

	// logs.Info("game:%v", cursor)
	// // 遍历查询结果
	// for cursor.Next(c) {
	// 	var game entity.Game
	// 	if err := cursor.Decode(&game); err != nil {
	// 		c.JSON(http.StatusInternalServerError, gin.H{"error": "Error decoding game"})
	// 		return
	// 	}
	// 	logs.Info("game:%v", game)
	// 	games = append(games, game)
	// }

}

func (g *GameApi) Edit(ctx *gin.Context) {

}
